package com.example.shixun1.renttest.main.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;


import androidx.appcompat.app.AppCompatActivity;

import com.example.shixun1.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//import android.widget.AdapterView;
//import android.widget.Toast;

public class Zhujiemian extends AppCompatActivity {
    private ListView lv;
    private List<Map<String,Object>> data;
    public static List<Map<String, Object>> car = new ArrayList<>();
    private SimpleAdapter sa;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zhujiemian);
        lv=findViewById(R.id.lv);
        init();


    }

    private void init() {
        data=new ArrayList<>();
        Map<String,Object>item=null;

        item=new HashMap<>();
        item.put("icon",R.drawable.a1);
        item.put("name","红楼梦");
        item.put("price",30);
        item.put("desc","都云作者痴，谁解其中味？");
        data.add(item);

        item=new HashMap<>();
        item.put("icon",R.drawable.a2);
        item.put("name","索尼A7M3");
        item.put("price",11140);
        item.put("desc","索尼全画幅新基准");
        data.add(item);

        item=new HashMap<>();
        item.put("icon",R.drawable.a3);
        item.put("name","小米Pro15.6");
        item.put("price",6699);
        item.put("desc","十代新品，金属机身");
        data.add(item);

        item=new HashMap<>();
        item.put("icon",R.drawable.a4);
        item.put("name","HUAWEI P40 Pro");
        item.put("price",5988);
        item.put("desc","24期免息");
        data.add(item);

        item=new HashMap<>();
        item.put("icon",R.drawable.a5);
        item.put("name","优衣库UNIQLO 轻型腰包 423485");
        item.put("price",79);
        item.put("desc","简约百搭");
        data.add(item);

        item=new HashMap<>();
        item.put("icon",R.drawable.a6);
        item.put("name","小米Redmi AirDots蓝牙耳机");
        item.put("price",99.9);
        item.put("desc","好音质,小巧甩不掉");
        data.add(item);

        item=new HashMap<>();
        item.put("icon",R.drawable.a7);
        item.put("name","百年孤独");
        item.put("price",89);
        item.put("desc","魔幻现实主义文学代表作");
        data.add(item);

        item=new HashMap<>();
        item.put("icon",R.drawable.a8);
        item.put("name","三体全集");
        item.put("price",107);
        item.put("desc","地球往事三部曲");
        data.add(item);

        sa=new SimpleAdapter(Zhujiemian.this,data,R.layout.activity_zhujirmian2,
                new String[]{"icon","name","price","desc"},
                new int[]{R.id.imageView,R.id.textView,R.id.textView2,R.id.textView3}){
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                final int p=position;
                final View view=super.getView(position, convertView, parent);
                Button useBtn=(Button)view.findViewById(R.id.button2);
                useBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        car.add(data.get(p));
                        Toast.makeText(Zhujiemian.this, "已添加", Toast.LENGTH_SHORT).show();
                    }
                });

                view.findViewById(R.id.button3).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Toast.makeText(Zhujiemian.this, "点赞", Toast.LENGTH_SHORT).show();
                    }
                });

                return view;
            }
        };




        lv.setAdapter(sa);



    }




    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(Menu.NONE, Menu.FIRST + 1, 1, "购物车");
        menu.add(Menu.NONE, Menu.FIRST + 2, 2, "退出登录");
        return true;
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        /**
         * 菜单项被点击时调用，也就是菜单项的监听方法。
         * 通过这几个方法，可以得知，对于Activity，同一时间只能显示和监听一个Menu 对象。 TODO Auto-generated
         * method stub
         */

        switch (item.getItemId()) {
            case Menu.FIRST + 1:
                Intent intent = new Intent(Zhujiemian.this, shopCartActivity.class);
                startActivity(intent);
                break;
            case Menu.FIRST + 2:
                finish();
                break;
            default:
                break;
        }

        return super.onOptionsItemSelected(item);
    }

}
